package com.feicheng.common.advice;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.mqtt.MqttInboundChannel;
import com.feicheng.service.impl.FMjConnectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Listener {

    @Autowired
    MqttInboundChannel inboundChannel;

    @Autowired
    FMjConnectionService fMjConnectionService;

    @Value("${mqtt.enable}")
    Boolean mqttEnabled;

    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("应用已启动并准备就绪！");



        if (mqttEnabled) {
            fMjConnectionService.runDzDailyTask();
//            JSONObject result = mjService.allMjByStoreAndRoom(new JSONObject());
//            JSONArray mjs = result.getJSONArray("amenjins");
//            for(int i = 0; i < mjs.size(); i++) {
//                JSONObject mj = mjs.getJSONObject(i);
//                String sn = mj.getString("sn");
//                inboundChannel.addTopic("mqtt/face/"+sn+"/Ack"); // 订阅设备响应频道
//                inboundChannel.addTopic("mqtt/face/"+sn+"/Rec"); // 订阅设备认证人员扫脸上传频道
//                inboundChannel.addTopic("mqtt/face/"+sn+"/Snap"); // 订阅设备陌生人扫脸上传频道
//                inboundChannel.addTopic("mqtt/face/"+sn+"/QRCode"); // 订阅设备二维码扫码上传频道
//            }
        }


    }
}
